package world.zwq.gulimall.ware.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import world.zwq.common.utils.PageUtils;
import world.zwq.common.utils.Query;

import world.zwq.common.utils.R;
import world.zwq.gulimall.ware.dao.WareSkuDao;
import world.zwq.gulimall.ware.entity.WareSkuEntity;
import world.zwq.gulimall.ware.feigin.ProductFeignService;
import world.zwq.gulimall.ware.service.WareSkuService;
import world.zwq.gulimall.ware.vo.SkuHasStockVo;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        String wareId = (String) params.get("wareId");
        if(StringUtils.isNotBlank(skuId)){
            wrapper.eq("sku_id",skuId);
        }
        if(StringUtils.isNotBlank(wareId)){
            wrapper.eq("ware_id",wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //判断如果没有库存记录，进行新增操作
        List<WareSkuEntity> list = this.baseMapper.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if(list == null || list.size() <= 0){
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setStock(skuNum);

            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStockLocked(0);
            // 远程查询sku名字，失败事务不需要回滚
            //1、自己捕获异常
            //2、TODO 还可以使用什么办法让异常出现以后不回滚？高级部分处理
            try {
                R info = productFeignService.info(skuId);
                if(info.getCode() == 0){
                    Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");
                    wareSkuEntity.setSkuName((String) data.get("skuName"));
                }
            } catch (Exception e) {
                //请求远程发生异常了
            }
            this.save(wareSkuEntity);
        } else {
            this.baseMapper.addStock(skuId,wareId,skuNum);
        }
    }

    @Override
    public List<SkuHasStockVo> getSkusHasStock(List<Long> skuIds) {
        List<SkuHasStockVo> list = skuIds.stream().map(skuId -> {
            SkuHasStockVo vo = new SkuHasStockVo();
            //查询库存量
            Long stock = this.baseMapper.selectSkuStock(skuId);
            vo.setSkuId(skuId);
            vo.setHasStock(stock==null ? false : stock > 0);
            return vo;
        }).collect(Collectors.toList());
        return list;
    }

}